﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SiteServer.Plugin;
using Thingpower.SS.EduOnline.Provider;
using Thingpower.SS.EduOnline.Model;

namespace Thingpower.SS.EduOnline.BackgroundPages
{
    public partial class PageCoursewareStudyStatistic : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!EduOnlinePlugin.Instance.AdminApi.HasSystemPermissions())
            {
                HttpContext.Current.Response.Write("<h1>未授权访问</h1>");
                HttpContext.Current.Response.End();
                return;
            }

            RptContents.DataSource = Provider.CoursewareStudyStatisticDAO.GetAllModelsList();
            RptContents.ItemDataBound += RptContents_ItemDataBound;
            RptContents.DataBind();
        }

        private void RptContents_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;

            var ltlIndex = (Literal)e.Item.FindControl("ltlIndex");
            var ltlCoursewareId = (Literal)e.Item.FindControl("ltlCoursewareId");
            var ltlCoursewareTitle = (Literal)e.Item.FindControl("ltlCoursewareTitle");
            var ltlCategory = (Literal)e.Item.FindControl("ltlCategory");
            var ltlUsersCount = (HyperLink)e.Item.FindControl("lnkUsersCount");
            var ltlTimesCount = (Literal)e.Item.FindControl("ltlTimesCount");

            var _model = (CoursewareStudyStatisticInfo)e.Item.DataItem;

            ltlIndex.Text = (e.Item.ItemIndex + 1).ToString();
            ltlCoursewareId.Text = _model.CoursewareId.ToString();
            ltlCoursewareTitle.Text = _model.CoursewareTitle.ToString();
            ltlCategory.Text = _model.Category == CoursewareCategory.Video ? "视频" : "PDF";
            ltlUsersCount.Text = _model.UsersCount.ToString();
            ltlUsersCount.NavigateUrl = $"/eduonline/api/statistic/getusersofcourseware?coursewareid={_model.CoursewareId.ToString()}";
            ltlTimesCount.Text = _model.TimesCount.ToString();

        }
    }
}